package fr.efrei.international.java.assignment.two;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.net.MalformedURLException;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JProgressBar;
import javax.swing.JTextField;
import javax.swing.Timer;

public class WelcomePage extends JFrame {

	Record record = null;
	FixedGlassPane glass;
	JProgressBar waiter = new JProgressBar(0, 100);
	Timer timer;
	char[] psw;
	JTextField userTextField;
	JPasswordField pswField;
	UsrRegister ur = new UsrRegister();
	UserData usr = new UserData(1);
	DataLoader dl = new DataLoader();

	public WelcomePage() {
		super("Chinese Learning System");
		setSize(500, 310);
		this.setDefaultCloseOperation(WelcomePage.EXIT_ON_CLOSE);
		this.setIconImage(Toolkit.getDefaultToolkit().createImage( "icon.jpg "));

		// set up a images for the main application
		JPanel mainPane = new JPanel();
		mainPane.setBackground(Color.white);
		mainPane.add(new JLabel(new ImageIcon("welcome.gif")));

		userTextField = new JTextField(10);
		pswField = new JPasswordField(10);
		pswField.setEchoChar('*');

		// Create labels and mnemonics
		JLabel userLabel = new JLabel("User", JLabel.RIGHT);
		userLabel.setDisplayedMnemonic('U');
		userLabel.setLabelFor(userTextField);
		JLabel pswLabel = new JLabel("Password", JLabel.RIGHT);
		pswLabel.setDisplayedMnemonic('P');
		pswLabel.setLabelFor(pswField);

		JPanel enterPanel = new JPanel();
		enterPanel.add(userLabel);
		enterPanel.add(userTextField);
		enterPanel.add(pswLabel);
		enterPanel.add(pswField);

		// Set up the glass pane with a little message and a progress bar...
		JPanel controlPane = new JPanel(new GridLayout(2, 1));
		// be opaque
		controlPane.setOpaque(false);
		controlPane.add(new JLabel("Please wait..."));
		controlPane.add(waiter);
		glass = new FixedGlassPane(getJMenuBar(), getContentPane());
		glass.setLayout(new GridLayout(0, 1));
		glass.setOpaque(false);

		glass.add(new JLabel());// padding...
		glass.add(new JLabel());// padding...
		glass.add(new JLabel());// padding...
		glass.add(new JLabel());// padding...

		glass.add(controlPane);
		setGlassPane(glass);

		// And last but not least, our button to launch the glass pane
		JButton startB = new JButton("Start your Chinese learning journey!");

		// startB.addKeyListener(new startButtonMonitor());
		startB.addKeyListener(new startButtonMonitor());
		startB.addActionListener(new startButtonMonitorAction());
		this.getRootPane().setDefaultButton(startB);

		JPanel btPanel = new JPanel(new GridLayout(2, 1));
		btPanel.add(enterPanel);
		btPanel.add(startB);

		Container contentPane = getContentPane();
		contentPane.add(mainPane, BorderLayout.CENTER);
		contentPane.add(btPanel, BorderLayout.SOUTH);

		// add MenuBar for welcome page
		add(new IMenuBar(ur), BorderLayout.NORTH);

		// show the frame at the center of the screen
		setLocationRelativeTo(null);
		pack();
		setVisible(true);
	}

	public boolean checkUsr() {
		boolean flag = false;
		String inputedUsr = userTextField.getText();
		String inputedPsw = new String(pswField.getPassword());
		if ((inputedUsr.length() == 0) || (inputedPsw.length() == 0)) {
			JOptionPane.showMessageDialog(this,
					"the username and password can not be empty!!", "Sorry",
					JOptionPane.WARNING_MESSAGE);
		} else
			flag = ur.checkUsr(inputedUsr, inputedPsw);
		return flag;

	}

	// progress bar
	public void startTimer() {
		if (timer == null) {
			timer = new Timer(1000, new ActionListener() {
				int progress = 0;

				public void actionPerformed(ActionEvent A) {
					progress += 50;
					waiter.setValue(progress);

					// Once we hit 100%, remove the glass pane and reset the
					// progress bar stuff
					if (progress >= 100) {
						progress = 0;
						timer.stop();
						glass.setVisible(false);
						// Again, manually control our 1.2/1.3 bug workaround
						glass.setNeedToRedispatch(true);
						waiter.setValue(0);
						record = new Record(userTextField.getText());
						new MainFrame(usr, dl, record);
						setVisible(false);
					}
				}
			});
		}
		if (timer.isRunning()) {
			timer.stop();
		}
		timer.start();
	}

	// method for play music

	private class startButtonMonitorAction implements ActionListener {

		public void actionPerformed(ActionEvent arg) {
			boolean isUser = checkUsr();
			System.out.println(isUser);
			if (isUser) {
				// manually control the 1.2/1.3 bug work-around
				glass.setNeedToRedispatch(false);
				glass.setVisible(true);
				startTimer();
				try {
					Music.Play("SoundTest.wav");
				} catch (MalformedURLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			} else {
				JOptionPane
						.showMessageDialog(
								null,
								"Sorry!\n\r Username do not exist! \n\r Please register a new Usr! ",
								"Sorry!", JOptionPane.WARNING_MESSAGE);
				userTextField.setText("");
				pswField.setText("");
			}
		}
	}

	private class startButtonMonitor implements KeyListener {

		public void keyPressed(KeyEvent ke) {
			if (ke.getKeyChar() == ke.VK_ENTER) {
				boolean isUser = checkUsr();
				System.out.println(isUser);
				if (isUser) {
					// manually control the 1.2/1.3 bug work-around
					glass.setNeedToRedispatch(false);
					glass.setVisible(true);
					startTimer();
					try {
						Music.Play("SoundTest.wav");
					} catch (MalformedURLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				} else {
					JOptionPane
							.showMessageDialog(
									null,
									"Sorry!\n\r Username do not exist! \n\r Please register a new Usr! ",
									"Sorry!", JOptionPane.WARNING_MESSAGE);
					userTextField.setText("");
					pswField.setText("");
				}
			}
		}

		@Override
		public void keyReleased(KeyEvent e) {
			// TODO Auto-generated method stub

		}

		@Override
		public void keyTyped(KeyEvent e) {
			// TODO Auto-generated method stub

		}

	}

}
